﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using FTchina;
//从我的新闻审阅系统里移植的
public partial class UI_member : System.Web.UI.Page
{
    string id = string.Empty;
    string grid = string.Empty;//记者等修改用户名的id
    protected void Page_Load(object sender, EventArgs e)
    {
        basic.CheckAdmin(this);
        basic.ChkPoewr(this, FTchina.Api.htmlpath.GetCurrentPagePath());
        id = basic.FilterSQL(Request["id"]);
        grid = FTchina.Library.MasterHelper.Luruid;
        if (!basic.ChkPoewr(this)) id = grid;//如果是个人登陆则将id赋值为本人id，即不能修改别人并避免id获取错误
        if (!IsPostBack)
        {

            if (id.Length != 0) { GetData(); lblflag.Visible = true; btnok.Text = "确定修改"; }
            else { lblflag.Visible = false; btnok.Text = "确定提交"; }
            bindData();
            BindPowerType();          
        }

        this.txbuname.Attributes.Add("ReadOnly", "true");//使用户名只读，只能通过选择权获取

    }


    /// <summary>
    /// 绑定权限类别
    /// </summary>
    protected void BindPowerType()
    {
        ddluquan.DataSource = shujuku.GetDataSet("select * from usergroup order by id asc");
        ddluquan.DataTextField = "groupname";
        ddluquan.DataValueField = "id";
        ddluquan.DataBind();
    }

     
    private void GetData()
    {
        string GetDataSql = "select * from Master where link=" + id + "";
        DataTable dt = shujuku.GetDataTable(GetDataSql);
        txbuname.Text = getUserNameByUID(id);
        txbuid.Text = dt.Rows[0]["link"].ToString().Trim();
        ddluquan.SelectedValue = dt.Rows[0]["power"].ToString().Trim();
        txbnicheng.Text = dt.Rows[0]["nicheng"].ToString().Trim();

    }
    protected void btnok_Click(object sender, EventArgs e)
    {
        if (btnok.Text == "确定提交") InsertUser();
        if (btnok.Text == "确定修改") UpDateUser();
        txbuname.Text = "";
        txbnicheng.Text = "";
    }
    //添加新用户
    protected void InsertUser()
    {
        string username = basic.FilterSQL(txbuname.Text);       
        string nicheng = basic.FilterSQL(txbnicheng.Text);

        if (username.Length == 0) { JS.Alert("请输入用户名"); return; }        
        if (nicheng.Length==0) { JS.Alert("请输入昵称"); return; }
        if (txbupsw.Text.Length == 0) { JS.Alert("请输入密码"); return; }
        if (txbupsw.Text != txbupsw2.Text) { JS.Alert("两次密码输入不一致！"); return; }

        string yuangongnema = txbuname.Text;
        string yuangongid = txbuid.Text;


        if (CheckNicheng(nicheng))
        { JS.Alert("此登录昵称已存在，请更换"); return; }



        string userpsw = MD5MK.MD5String(basic.FilterSQL(txbupsw.Text));
        string power = ddluquan.SelectedValue;
        string pname = ddluquan.SelectedItem.Text;

        string InsertSql = "insert into Master (nicheng,Master_Name,Master_Password,power,pname,link,state) values ('" + nicheng + "','" + yuangongnema + "','" + userpsw + "','" + power + "','" + pname + "','" + yuangongid + "','1')";
        UImsg.PageRender(shujuku.ExecuteSql(InsertSql));

    }
    //修改用户
    protected void UpDateUser()
    {

        string nicheng = basic.FilterSQL(txbnicheng.Text);
        string username = basic.FilterSQL(this.txbuname.Text);
        string userid=basic.FilterSQL(this.txbuid.Text);
        if (nicheng.Length == 0) { JS.Alert("请输入登录昵称"); return; }
        if (userid.Length == 0 || username.Length==0) { JS.Alert("请输入用户名"); return; }
        string power = ddluquan.SelectedValue;
        string pname = ddluquan.SelectedItem.Text;
        string UpDateSql = string.Empty;
        if (nicheng != getNichengById(id))
        {
            if (CheckNicheng(nicheng))
            { JS.Alert("此登录昵称已存在，请更换"); return; }
        }
        if (txbupsw.Text.Length == 0 & txbupsw2.Text.Length == 0)
        {
            UpDateSql = "update Master set link='"+userid+"', Master_Name='" + username + "', nicheng='" + nicheng + "',power='" + power + "',pname='" + pname + "' where link=" + id;
        }
        else
        {
            if (txbupsw.Text != txbupsw2.Text) { JS.Alert("两次密码输入不一致！"); return; }
            if (txbupsw.Text.Length == 0) { JS.Alert("请输入密码"); return; }
            string userpsw = MD5MK.MD5String(basic.FilterSQL(txbupsw.Text));
            UpDateSql = "update Master set link='" + userid + "',  Master_Name='" + username + "',nicheng='" + nicheng + "',Master_Password='" + userpsw + "',power='" + power + "',pname='" + pname + "' where link=" + id;
        }
        shujuku.ExecuteSql(UpDateSql);
        if (id == FTchina.Library.MasterHelper.Luruid)
        {
            FTchina.Library.RequestHelper.RemoveAllCookies();

            Response.Redirect("login.aspx?title=修改信息，重新登陆");
        }
        else
        {
            UImsg.PageRender(true);
        }
 

    }



    /// <summary>
    /// 检查昵称是否存在
    /// </summary>
    /// <param name="nicheng"></param>
    /// <returns></returns>
    protected String getNichengById(string id)
    {
        string sql = "select nicheng from Master where  link='" + id + "'";
        return shujuku.GetDataTable(sql).Rows[0][0].ToString();
    }
    /// <summary>
    /// 检查昵称是否存在
    /// </summary>
    /// <param name="nicheng"></param>
    /// <returns></returns>
    protected Boolean CheckNicheng(string nicheng)
    {
        string testread = "select Master_Name from Master where  nicheng='" + nicheng + "'";
        return shujuku.testread(testread);
    }
    /// <summary>
    /// 通过UID获取用户姓名
    /// </summary>
    /// <param name="uid"></param>
    /// <returns></returns>
    public String getUserNameByUID(string uid)
    {
        try
        {
            string sql = "select Master_Name from userlist where id='" + uid + "'";
            return shujuku.GetDataTable(sql).Rows[0][0].ToString();
        }
        catch (Exception e)
        {
            return "";
        }
    }

    protected void bindData()
    {
        string GetDateSql = "select * from Master order by id desc";
        DataSet ds = shujuku.GetDataSet(GetDateSql);
        gvList.DataSource = ds;
        gvList.DataBind();
        LblCurrentIndex.Text = (gvList.PageIndex + 1).ToString();
        LblPageCount.Text = gvList.PageCount.ToString();
        LblRecordCount.Text = ds.Tables[0].Rows.Count.ToString();
        if (ds.Tables[0].Rows.Count == 0)
        {
            btnFirst.Visible = false;
            btnPrev.Visible = false;
            btnNext.Visible = false;
            btnLast.Visible = false;

            LblCurrentIndex.Visible = false;
            LblPageCount.Visible = false;
            LblRecordCount.Visible = false;
            Label1.Visible = false;
            Label1.Visible = false;
            Label2.Visible = false;
            Label3.Visible = false;
            Label4.Visible = false;
            Label5.Visible = false;
            Label6.Visible = false;

            //lbMessage.Text = "没有记录！";
        }
        else if (gvList.PageCount == 1)
        {
            btnFirst.Visible = false;
            btnPrev.Visible = false;
            btnNext.Visible = false;
            btnLast.Visible = false;
        }
        // 计算生成分页页码,分别为："首 页" "上一页" "下一页" "尾 页"
        btnFirst.CommandName = "1";
        btnPrev.CommandName = (gvList.PageIndex == 0 ? "1" : gvList.PageIndex.ToString());
        btnNext.CommandName = (gvList.PageCount == 1 ? gvList.PageCount.ToString() : (gvList.PageIndex + 2).ToString());
        btnLast.CommandName = gvList.PageCount.ToString();

    }
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        gvList.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
        bindData();

    }
    protected void gvList_PageIndexChanging(Object sender, GridViewPageEventArgs e)
    {
        if (gvList.PageCount > 1)
        {
            gvList.PageIndex = e.NewPageIndex;
            bindData();
        }
        else
        {
            LblCurrentIndex.Text = gvList.EditIndex.ToString();
        }
    }

    

}
